<?php
class Stat
{
    private $property = array();
    private $valid = false;
    private $valid_properties = "StatID,Filename,DownloadCount";

    public static function createFromArray($values)
    {
        if (!is_array($values)) return null;
        if (count($values) <= 0) return null;

        $S = new Stat();
        $S->FromArray($values);
        return $S;
    }

    public static function createFromCommand(command $cmd)
    {
        logger::called();

        if (!is_array($cmd->Data())) return null;
        if (count($cmd->Data()) <= 0) return null;

        $S = new Stat();
        $S->FromArray($cmd->Data());
        return $S;
    }
    
    public function __construct(){}

    public function StatID()
    {
        return $this->GetSetData("StatID",null);
    }
    
    public function Filename()
    {
        return $this->GetSetData("Filename",null);
    }
    
    public function DownloadCount()
    {
        return $this->GetSetData("DownloadCount",null);
    }

    public function StorableData()
    {
        $result["StatID"] = $this->StatID();
        $result["Filename"] = $this->Filename();
        $result["DownloadCount"] = $this->DownloadCount();

        return $result;
    }

    public function UpdateStatToDatabase()
    {
        $result = ns_dao_software::updateStatByArray($this->property["StatID"], $this->property);
        return $result;
    }

    private function GetSetData($property_name,$value = null)
    {
        //if (!$this->checkPropertyName($property_name)) return null;
        if (is_null($value)) return $this->property[$property_name];
        $this->property[$property_name] = $value;
    }

    private function checkPropertyName($property_name)
    {
        foreach (explode(",",$this->valid_properties) as $allowed_prop)
            if ($allowed_prop == $property_name) return true;

        return false;
    }


    /*
     * Populate from an array
     */
    public function FromArray($values)
    {
        if (!is_array($values)) return null;

        foreach ($values as $key => $value)
            $this->GetSetData($key,$value);

    }


    public function __toString()
    {
        return print_r($this->property,true);
    }


}

?>
